home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  8KB  |  192 lines

  1. /*
  2. **  PushDir() and PopDir()
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is an expanded version of the one
  8. **  originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <dos.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #define DIR_STACK_SIZE  8
  17. #define MAX_FLEN        67
  18.  
  19. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  20.  
  21. #define BOOL(x) (!(!(x)))
  22.  
  23. /*
  24. **  NOTE: Uses the author's chdrv(), also in SNIPPETS!
  25. */
  26.  
  27. int chdrv(int);
  28.  
  29. static int  PushDir_stack_ptr;
  30. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  31.  
  32. /************************************************************************/
  33. /*                                                                      */
  34. /*  PushDir()                                                           */
  35. /*                                                                      */
  36. /*  Like chdir(), except a drive may be specified and the old directory */
  37. /*  is saved.                                                           */
  38. /*                                                                      */
  39. /*  Arguments: 1 - newdir, the buffer containing the new directory name */
  40. /*                                                                      */
  41. /*  Returns:  -1 - stack overflow                                       */
  42. /*             0 - error                                                */
  43. /*             1 - success, still on same drive                         */
  44. /*             2 - success, changed drive                               */
  45. /*                                                                      */
  46. /*  Side effects: Converts name in newdir to upper case and prepends    */
  47. /*                a drive letter.                                       */
  48. /*                                                                      */
  49. /*  CAUTION: Since a drive will be prepended to newdir, it's buffer     */
  50. /*           should be at at least MAX_FLEN long.                       */
  51. /*                                                                      */
  52. /************************************************************************/
  53.  
  54. int PushDir(char *newdir)
  55. {
  56.       char pname[MAX_FLEN];
  57.       char drive[3];
  58.       char *target = &pname[2];
  59.       int i, new_drv = 0, ercode = 0;
  60.       static int init = 0;
  61.  
  62.       if (!init)
  63.             PushDir_stack_ptr = init = -1;
  64.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  65.       {
  66.             ercode = -1;
  67.             goto ErrEx;
  68.       }
  69.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  70.       strupr(PushDir_stack[PushDir_stack_ptr]);
  71.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  72.       drive[2] = '\0';
  73.       if (':' == newdir[1])
  74.       {     /* If a drive is specified                                  */
  75.             strupr(newdir);
  76.             strcpy(pname, newdir);
  77.             if (strchr(target, ':'))      /* if filename is illegal     */
  78.                   goto ErrEx;
  79.             if (*drive != *newdir)
  80.             {
  81.                   if (ERROR == chdrv(newdir[0]))
  82.                   {     /* If the drive is invalid                      */
  83.                         goto ErrEx;
  84.                   }
  85.                   else  new_drv = 1;
  86.             }
  87.       }
  88.       else
  89.       {     /* If a drive isn't specified                               */
  90.             if (!strchr(strupr(newdir), ':'))
  91.             {     /* If legal filename                                  */
  92.                   strcpy(pname, drive);
  93.                   strcat(pname, newdir);
  94.                   strcpy(newdir, pname);
  95.             }
  96.             else
  97.             {     /* If filename is illegal                             */
  98.                   goto ErrEx;
  99.             }
  100.       }
  101.  
  102.       if (*target)
  103.       {
  104.             if (chdir(target))
  105.             {
  106.                   if (1 == new_drv) /* We already changed drives        */
  107.                         chdrv(*drive);    /* Go home before exit        */
  108.                   goto ErrEx;
  109.             }
  110.       }
  111.       return (new_drv + 1);
  112. ErrEx:
  113.       --PushDir_stack_ptr;
  114.       return (ercode);
  115. }
  116.  
  117. /************************************************************************/
  118. /*                                                                      */
  119. /*  PopDir()                                                            */
  120. /*                                                                      */
  121. /*  Like chdir(), except goes to the drive/directory specified on the   */
  122. /*  top of the PushDir stack.                                           */
  123. /*                                                                      */
  124. /*  Arguments: none                                                     */
  125. /*                                                                      */
  126. /*  Returns:  -1 - stack empty                                          */
  127. /*             0 - error - stack pointer unchanged                      */
  128. /*             1 - success, still on same drive                         */
  129. /*             2 - success, changed drive                               */
  130. /*                                                                      */
  131. /*  Side effects: none                                                  */
  132. /*                                                                      */
  133. /*  CAUTION: chdir() or chdrv() should not be called between PushDir-   */
  134. /*           PopDir calls.                                              */
  135. /*                                                                      */
  136. /************************************************************************/
  137.  
  138. int PopDir(void)
  139. {
  140.       char I_am_here[MAX_FLEN], target_drv, *target;
  141.       int new_drv = 0;
  142.  
  143.       if (0 > PushDir_stack_ptr)
  144.             return -1;
  145.       getcwd(I_am_here, MAX_FLEN);
  146.       target = &PushDir_stack[PushDir_stack_ptr][2];
  147.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  148.       if (I_am_here[0] != target_drv)
  149.       {
  150.             if (ERROR == chdrv(target_drv))
  151.                   return 0;
  152.             new_drv = 1;
  153.       }
  154.       if (!chdir(target))
  155.       {
  156.             --PushDir_stack_ptr;
  157.             return (1 + new_drv);
  158.       }
  159.       else  return 0;
  160. }
  161.  
  162. /************************************************************************/
  163. /*                                                                      */
  164. /*  isdir()                                                             */
  165. /*                                                                      */
  166. /*  Checks to see if a drive and/or path are a valid directory.         */
  167. /*                                                                      */
  168. /*  Arguments: 1 - dir, the buffer containing the new directory name    */
  169. /*                                                                      */
  170. /*  Returns: ERROR - push/popdir stack overflow                         */
  171. /*           FALSE - not a valid directory                              */
  172. /*           TRUE  - valid directory                                    */
  173. /*                                                                      */
  174. /*  Side effects: Converts name in dir to upper case and prepends a     */
  175. /*                drive letter.                                         */
  176. /*                                                                      */
  177. /*  CAUTION: Since a drive will be prepended to newdir, it's buffer     */
  178. /*           should be at at least MAX_FLEN (see MFLFILES.H) long.      */
  179. /*                                                                      */
  180. /************************************************************************/
  181.  
  182. int isdir(char *dir)
  183. {
  184.       int ercode;
  185.  
  186.       if (-1 == (ercode = pushdir(dir)))
  187.             return ercode;
  188.       if (ercode)
  189.             popdir();
  190.       return BOOL(ercode);
  191. }
  192.